define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var activityOrder = new Vue({
                el: "#activityOrder",
                data() {
                    return {
                        id: '',
                        orderList: [],
                        tableCheckedAll: false,
                        isIndeterminate: false,
                        multipleTable: [],
                        currentPage: 1,
                        totalPage: 0,
                        offset: 0,
                        limit: 10,
                        // form搜索
                        filterRule: {
                            status: [{name: "全部",type: "all"},{name: "待领取",type: "0"},{name: "待审核",type: "1"},{name: "已审核",type: "2"}],
                        },
                        searchForm: {
                            status: "all",
                            form_1_key: "user_id",
                            form_1_value: "",
                        },
                        searchFormInit: {
                            status: "all",
                            form_1_key: "user_id",
                            form_1_value: "",
                        },
                        searchOp: {
                            status: "=",
                            user_id: "=",
                            user_nickname: "like",
                            user_mobile: "like"
                        },
                        // 同意
                        agreeDialogVisible: false,
                        agreeRow: {},
                        agreeForm: {
                           reward: 0
                        },
                        // 拒绝
                        refuseDialogVisible: false,
                        refuseRow: {},
                        refuseForm: {
                            status: '',
                            status_msg: ''
                        },
                    }
                },
                mounted() {
                    var params = new URLSearchParams(window.location.search);
                    this.id = params.get('ids'); 
                    this.getActivityOrder()
                },
                methods: {
                    getActivityOrder(offset = 0, limit = 10) {
                        var that = this;
                        that.offset = offset;
                        that.limit = limit;
                        let filter = {}
                        let op = {}
                        for (key in that.searchForm) {
                            if (key == 'status') {
                                if (that.searchForm[key] !== '' && that.searchForm[key] != 'all') {
                                    filter[key] = that.searchForm[key];
                                }
                            } else if (key == 'form_1_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_1_key] = that.searchForm[key];
                                }
                            } else if (key == 'createtime' || key == 'updatetime') {
                                if (that.searchForm[key]) {
                                    if (that.searchForm[key].length > 0) {
                                        filter[key] = that.searchForm[key].join(' - ');
                                    }
                                }
                            }
                        }
                        for (key in filter) {
                            op[key] = that.searchOp[key]
                        }
                        // return false;
                        Fast.api.ajax({
                            url: 'shopro/activity/activity_order/index',
                            loading: true,
                            type: 'GET',
                            data: {
                                filter: JSON.stringify(filter),
                                op: JSON.stringify(op),
                                offset: offset,
                                limit: limit,
                                id: that.id
                            }
                        }, function (ret, res) {
                            var list = res.data.rows;
                            for(var i = 0;i < list.length;i++){
                                if(list[i].agent && list[i].agent.apply_info){
                                    var apply_info = JSON.parse(list[i].agent.apply_info);
                                    var _apply_info = {}
                                    for(var j = 0;j < apply_info.length;j++){
                                        _apply_info[apply_info[j]['key']] = apply_info[j]['value']
                                    }
                                    list[i].agent.apply_info = _apply_info
                                }
                            }
                            console.log(list)
                            that.orderList = list
                            that.totalPage = res.data.total;
                            return false;
                        })
                    },
                    handleSizeChange(val) {
                        this.currentPage = 1;
                        this.getActivityOrder(0, val)
                    },
                    handleCurrentChange(val) {
                        this.currentPage = val;
                        this.getActivityOrder((val - 1) * this.limit, this.limit)
                    },
                    filterEmpty() {
                        this.searchForm = JSON.parse(JSON.stringify(this.searchFormInit));
                        this.currentPage = 1;
                        this.getActivityOrder(0, 0);
                    },
                    filterConfirm() {
                        this.currentPage = 1;
                        this.getActivityOrder(0, 10)
                    },
                    changeApplyStatus(tab, event) {
                        this.currentPage = 1;
                        this.getActivityOrder(0, 10)
                    },
                    changeCheckedAll(val) {
                        if (val) {
                            this.orderList.forEach(row => {
                                this.$refs.multipleTable.toggleRowSelection(row, true);
                            });
                            this.isIndeterminate = false;
                        } else {
                            this.$refs.multipleTable.clearSelection();
                        }
                    },
                    tableSelect(val) {
                        this.multipleSelection = val;
                        this.tableCheckedAll = this.multipleSelection.length == 0 ? false : true
                        this.isIndeterminate = (this.multipleSelection.length == 0 || this.multipleSelection.length == this.orderList.length) ? false : true;
                    },
                    checkSelectable(row) {
                        return this.searchForm.status == 0 || this.searchForm.status == 1 ? true : false
                    },
                    // 同意
                    openAgreeDialog(row) {
                        this.agreeDialogVisible = true;
                        this.agreeRow = row;
                    },
                    agreePayment() {
                        this.reqApplyOper(this.agreeRow, 1, this.agreeForm.reward)
                    },
                    closeAgreeDialog() {
                        this.agreeDialogVisible = false;
                    },
                    // 拒绝
                    openRefuseDialog(row) {
                        this.refuseDialogVisible = true;
                        this.refuseRow = row;
                    },
                    refusePayment() {
                        this.reqApplyOper(this.refuseRow, -1, this.refuseForm.status_msg)
                    },
                    closeRefuseDialog() {
                        this.refuseDialogVisible = false;
                        this.refuseForm.status_msg = "";
                    },
                    reqApplyOper(row, status, status_msg) {
                        let that = this;
                        let ids = ''
                        if (Array.isArray(row)) {
                            let idsArr = []
                            row.forEach(m => {
                                idsArr.push(m.id)
                            })
                            ids = idsArr.join(',')
                        } else {
                            ids = row.id
                        }
                        let reaData = {
                            operate: status
                        }
                        if (status == 1) {
                            reaData['reward'] = status_msg
                        } else if (status == -1) {
                            reaData = {
                                operate: status,
                                rejectInfo: status_msg
                            }
                        }
                        Fast.api.ajax({
                            url: `shopro/activity/activity_order/handle/ids/${ids}`,
                            loading: true,
                            type: 'POST',
                            data: reaData //同意 1  立即打款 2   同意&打款3
                        }, function (ret, res) {
                            that.getActivityOrder(that.offset, that.limit);
                            that.agreeDialogVisible = false
                            that.refuseDialogVisible = false;
                            that.refuseForm.status_msg = "";
                            return false;
                        })
                    },
                    openUser(id) {
                        Fast.api.open('shopro/user/user/profile?id=' + id, '查看')
                    }
                },
            })
        },
    };
    return Controller;
});